﻿using GedeaProject.Business.Parsers.Helpers;
using GedeaProject.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GedeaProject.Business.Parsers
{
    internal class TransactionTextDebitParser
    {
        private Transaction transaction;
        private CustomerTextDebitParser customerParser;
        private CompanyTextDebitParser companyParser;

        internal TransactionTextDebitParser(Transaction transaction, Company company)
        {
            this.transaction = transaction;
            this.companyParser = new CompanyTextDebitParser(company); ;
            this.customerParser = new CustomerTextDebitParser(this.transaction.Customer);
        }

        internal string GetFormattedText(int registryIndex)
        {
            StringBuilder resultBuilder = new StringBuilder();

            resultBuilder.Append(this.customerParser.InternalBankCode);
            resultBuilder.Append(this.ExpirationDate);
            resultBuilder.Append(this.InternalTransactionReference);
            resultBuilder.Append(this.customerParser.IdForBank);
            resultBuilder.Append("P");
            resultBuilder.Append(this.customerParser.Cbu);
            resultBuilder.Append(this.Amount);
            resultBuilder.Append(this.companyParser.Cuit);
            resultBuilder.Append(this.companyParser.Benefit);
            resultBuilder.Append(this.customerParser.Area);
            resultBuilder.Append(this.GetCount(registryIndex));
            resultBuilder.Append(TextDebitParserFormatter.StringFormat("", 5, ' ', true));
            resultBuilder.Append(TextDebitParserFormatter.StringFormat("", 15, ' ', true));
            resultBuilder.Append(TextDebitParserFormatter.StringFormat("", 22, ' ', true));//Nuevo identificador del cliente
            resultBuilder.Append(TextDebitParserFormatter.StringFormat("", 3, ' ', true)); //Codigo del rechazo
            resultBuilder.Append(this.companyParser.NameForBank);
            resultBuilder.Append(Environment.NewLine);

            return resultBuilder.ToString();
        }

        private string GetCount(int registryIndex)
        {
            return TextDebitParserFormatter.IntFormat(registryIndex + 1, 3);
        }

        private string InternalTransactionReference
        {
            get
            {
                return TextDebitParserFormatter.StringFormat("", 6, '0', true);
            }
        }

        public string ExpirationDate
        {
            get
            {
                return TextDebitParserFormatter.IntFormat(this.transaction.ExpirationDate.Day, 2)
                + TextDebitParserFormatter.IntFormat(this.transaction.ExpirationDate.Month, 2)
                + TextDebitParserFormatter.IntFormat(this.transaction.ExpirationDate.Year, 4);
            }
        }

        private string Amount
        {
            get
            {
                return TextDebitParserFormatter.StringFormat(
                    this.transaction.Amount.ToString("F").Replace(",", String.Empty), 10, '0', true);
            }
        }
    }
}
